package com.wise.site.adapter;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.wise.site.R;
import com.wise.site.protocol.action.CommentAction;
import com.wise.site.protocol.base.SoapAction.ActionListener;
import com.wise.site.protocol.base.ProtocolManager;
import com.wise.site.protocol.base.SoapItem;
import com.wise.site.protocol.base.SoapAction.ACTION_TYPE;
import com.wise.site.protocol.result.CommentItem;
import com.wise.site.protocol.result.CommentItemsResult;
import com.wise.site.widget.PullRefenceList;
import com.wise.site.widget.PullRefenceList.OnRefreshListener;

public class CommentAdapter extends ArrayListAdapter<SoapItem> {
	
	private CommentAction mAction; 
	private int page = 1;

	public CommentAdapter(Context context) {
		super(context);
		mAction = new CommentAction(ACTION_TYPE.ACTION_COMMON, "getcomments");
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if(convertView == null){
			convertView = inflater.inflate(R.layout.list_comment_item, null);
		}
		CommentItem item = (CommentItem) mList.get(position);
		convertView.setTag(item);
		((TextView)convertView.findViewById(R.id.comment_userName)).setText(item.getUserName());
		((TextView)convertView.findViewById(R.id.comment_date)).setText(item.getDate());
		((TextView)convertView.findViewById(R.id.comment_content)).setText(item.getContent());
		return convertView;
	}
	
	public void moveToFristPage(){
		page = 1;
		mAction.setPage(page);
	}
	
	public void moveToNextPage(){
		mAction.setPage(page);
	}
	
	public void setType(int type){
		mAction.addJsonParam("type", type);
	}
	
	public void setSubjectId(int subjectId){
		mAction.addJsonParam("SubjectId", subjectId);
	}
	
	public void loadData(){
		loadData(false);
	}
	
	
	public void loadData(final boolean isAdd){
		showProgress();
		
		mAction.setActionListener(new ActionListener<CommentItemsResult>() {

			@Override
			public void onSucceed(CommentItemsResult result) {
				// TODO Auto-generated method stub
				if(isAdd){
					addItems(result.list);
				} else {
					setList(result.list);
				}
				refenceComplete(result.list);
				page ++;
			}

			@Override
			public void onError(int resultCode) {
				// TODO Auto-generated method stub
				refenceComplete(null);
			}
		});
		
		ProtocolManager.getProtocolManager().submitAction(mAction);
	}

	@Override
	public void setListView(ListView listView) {
		super.setListView(listView);
		listView.setAdapter(this);
		if(mListView instanceof PullRefenceList){
			((PullRefenceList)mListView).setonRefreshListener(new OnRefreshListener() {
				@Override
				public void onRefresh() {
					removeAll();
					moveToFristPage();
					loadData();
				}
			});
			
			((PullRefenceList)mListView).setFootOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					moveToNextPage();
					loadData(true);
				}
			});
		}
	}
}
